package com.hnzq.cloud.freshuserassembly.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hnzq.cloud.UserAPI;
import com.hnzq.cloud.dto.PageUsersDto;
import com.hnzq.cloud.dto.RegisterDto;
import com.hnzq.cloud.dto.UserDto;
import com.hnzq.cloud.freshuserassembly.service.IFreUserService;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.*;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周皓月
 * @since 2022-02-03
 */

@RestController
@RequestMapping("/freUser")
public class FreUserController implements UserAPI {

    @Resource
    private IFreUserService fus;

    /**
     * 未登录跳转的页面
     * @return
     */
    @Override
    public Result noneLogin() {
        return Result.newInstance().setCode(405).setMessage("未登录");
    }

    /**
     * 用户注册或者后台新增用户
     * @param registerParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<RegisterDto> register(@RequestBody RegisterParam registerParam)  {
        RegisterDto registerDto = fus.register(registerParam);
        if (registerDto != null){
            return Result.newInstance().setMessage("注册成功").setData(registerDto);
        }else {
            return Result.newInstance().setMessage("注册失败").setCode(500);
        }
    }

    /**
     * 后台分页条件查询用户列表
     * @param findUsersParam
     * @return
     */
    @Override
    public Result<PageUsersDto> findUsersByCondition(FindUsersParam findUsersParam) {
        return Result.newInstance().setMessage("查询成功").setData(fus.findUsersByCondition(findUsersParam));
    }

    /**
     * 后台更新用户
     * @param updateUserParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<UserDto> updateUserById(UpdateUserParam updateUserParam) throws JsonProcessingException {
        return Result.newInstance().setMessage("更新成功").setData(fus.updateUserById(updateUserParam));
    }

    /**
     * 后台删除用户
     * @param deleteUserParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<Boolean> deleteUserById(DeleteUserParam deleteUserParam) throws JsonProcessingException {
        if (fus.deleteUserById(deleteUserParam)){
            return Result.newInstance().setMessage("删除成功");
        }
        return Result.newInstance().setMessage("删除失败");
    }

    /**
     * 后台增加用户积分
     * @param addUserScoreParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<Boolean> addUserScore(AddUserScoreParam addUserScoreParam) throws JsonProcessingException {
        if ( fus.addUserScore(addUserScoreParam)){
            return Result.newInstance().setMessage("增加积分成功");
        }
        return Result.newInstance().setCode(500).setMessage("增加积分失败");
    }

    /**
     * 后台计算用户等级
     * @param userLevelParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<Boolean> caculateUserLevel(UserLevelParam userLevelParam) throws JsonProcessingException {
        if (fus.caculateUserLevel(userLevelParam)){
            return Result.newInstance().setMessage("计算等级成功");
        }
        return Result.newInstance().setCode(500).setMessage("计算等级失败");
    }

    /**
     * 登录
     * @param accountName
     * @param accountPassword
     * @return
     */
    @Override
    public Result<UserDto> login(String accountName, String accountPassword) {
        String token = fus.login(accountName, accountPassword);
        return Result.newInstance().setCode(200).setMessage("登录成功").setData(token);
    }

    @Override
    public Result loginOut() {
        fus.loginOut();
        return Result.newInstance().setCode(200).setMessage("已退出登录状态");
    }


}

